//
//  MainViewController.swift
//  Weibo-DG-test
//
//  Created by Lang on 10/8/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    // 设置主题的颜色
    private func setThemeColor(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
       
//        setThemeColor()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setupComposedButton()
    }
   
    
    // 加号按钮
    private func setupComposedButton(){
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
    
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    func clickComposeButton(){
        print(__FUNCTION__)
    }
    
    /**
    添加子控制器
    */
    func addChildViewControllers() {
        
        // 只是添加控制器，并没有创建tabbar的 button
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
   
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        tabBar.tintColor = UIColor.orangeColor()
        
        // title 属性会从内向外传递设置
        vc.title = title
        
        
        // 设置图标
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
        
    }

    //  mark - 懒加载一个控件
    
    lazy private var composeButton: UIButton = {
        // 自定义类型的按钮
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 普通状态按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        // 高亮状态按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        // 添加监听方法
        button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(button)
        
        return button
    }()
    
}
